﻿#region Copyright Info

// 
// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using DesignByContract;

#endregion

namespace Majesty_of_Omega.GUI.Controls
{
    /// <summary>
    /// Interaction logic for MooPercentageBar.xaml
    /// This control simulates the percentage slider for research points in the master of orion technology screens.
    /// See also:
    /// http://www.moo3.at/gallery/details.php?image_id=126
    /// </summary>
    public partial class MooPercentageBar
    {
        #region PercentageChangedEvent

        /// <summary>
        /// Create a custom routed event by first registering a RoutedEventID
        /// This event uses the bubbling routing strategy
        /// </summary>
        public static readonly RoutedEvent PercentageChangedEvent = EventManager.RegisterRoutedEvent(
            "PercentageChanged", RoutingStrategy.Bubble, typeof (RoutedPropertyChangedEventHandler<int>),
            typeof (MooPercentageBar));

        /// <summary>
        /// Occurs when the Percentage value of the slider changed.
        /// </summary>
        public event RoutedPropertyChangedEventHandler<int> PercentageChanged
        {
            add { AddHandler(PercentageChangedEvent, value); }
            remove { RemoveHandler(PercentageChangedEvent, value); }
        }

        /// <summary>
        /// Invokes the percentage changed event.
        /// </summary>
        /// <param name="e">The <see cref="double"/> instance containing the event data.</param>
        private void InvokePercentageChanged(RoutedPropertyChangedEventArgs<double> e)
        {
            RaiseEvent(new RoutedPropertyChangedEventArgs<int>(Convert.ToInt32(e.OldValue), Convert.ToInt32(e.NewValue),
                                                               PercentageChangedEvent));
        }

        #endregion

        #region Dep.Report PercentageValue

        /// <summary>
        /// Gets or sets the percentage value with data binding
        /// </summary>
        /// <value>The percentage value.</value>
        public int PercentageValue
        {
            get { return (int)GetValue(PercentageValueProperty); }
            set { SetValue(PercentageValueProperty, value); }
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for the PercentageValue. This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty PercentageValueProperty =
            DependencyProperty.Register("PercentageValue", typeof(int), typeof(MooPercentageBar), new UIPropertyMetadata(0));

        #endregion

        /// <summary>
        /// Change value, for increment or decrementing the slider
        /// </summary>
        private const double _changeValue = 1.0;

        /// <summary>
        /// Initializes a new instance of the <see cref="MooPercentageBar"/> class.
        /// </summary>
        public MooPercentageBar()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Decrease the value of the Percentage bar with the "change" value
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void LeftButton_Click(object sender, RoutedEventArgs e)
        {
            Slider.Value = Math.Max(0, Slider.Value - _changeValue);
        }

        /// <summary>
        /// Increase the value of the Percentage bar with the "change" value
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void RightButton_Click(object sender, RoutedEventArgs e)
        {
            Slider.Value = Math.Min(100.0, Slider.Value + _changeValue);
        }

        /// <summary>
        /// Handles the ValueChanged event of the Slider control. The PercentageChanged Event will be invoked
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="double"/> instance containing the event data.</param>
        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            InvokePercentageChanged(e);
        }
    }

    /// <summary>
    /// Converts a value from the slider to a textual representation
    /// </summary>
    public class MooPercentageTextConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Converts a value from the slider to a textual representation
        /// </summary>
        /// <param name="valueObject">The value produced by the slider.</param>
        /// <param name="targetType">A text string binding target property.</param>
        /// <param name="parameter">no converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object Convert(object valueObject, Type targetType, object parameter, CultureInfo culture)
        {
            Require.IsTrue(targetType == typeof (String), "The target must be string property");
            return String.Format("{0:P0}", (double) valueObject/100.0);
        }

        /// <summary>
        /// This method is not used
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}